package com.campus.service.impl;

import com.campus.domain.dto.AnnouncementQueryDTO;
import com.campus.domain.dto.PublishAnnouncementDTO;
import com.campus.domain.entity.Announcement;
import com.campus.domain.entity.User;
import com.campus.domain.vo.AnnouncementVO;
import com.campus.domain.vo.PageResultVO;
import com.campus.mapper.AnnouncementMapper;
import com.campus.mapper.UserMapper;
import com.campus.service.IAnnouncementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnouncementServiceImpl implements IAnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void publishAnnouncement(PublishAnnouncementDTO dto, Long userId) {
        // 获取用户信息
        User user = userMapper.findById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 创建公告对象
        Announcement announcement = new Announcement();
        announcement.setTitle(dto.getTitle());
        announcement.setContent(dto.getContent());
        announcement.setPublisherId(userId);
        announcement.setPublisherName(user.getUserName());
        announcement.setViewCount(0);
        announcement.setStatus(1); // 1-已发布

        // 保存公告
        announcementMapper.insert(announcement);
    }

    @Override
    public PageResultVO<AnnouncementVO> getAnnouncementList(AnnouncementQueryDTO queryDTO) {
        // 计算偏移量
        int offset = (queryDTO.getPage() - 1) * queryDTO.getSize();

        // 查询公告列表
        List<Announcement> announcements = announcementMapper.selectList(
            queryDTO.getKeyword(),
            offset,
            queryDTO.getSize()
        );

        // 转换为VO
        List<AnnouncementVO> voList = announcements.stream().map(announcement -> {
            AnnouncementVO vo = new AnnouncementVO();
            BeanUtils.copyProperties(announcement, vo);
            return vo;
        }).collect(Collectors.toList());

        // 查询总数
        Long total = announcementMapper.countTotal(queryDTO.getKeyword());

        return new PageResultVO<>(voList, total, queryDTO.getPage(), queryDTO.getSize());
    }

    @Override
    @Transactional
    public AnnouncementVO getAnnouncementDetail(Long id) {
        // 查询公告
        Announcement announcement = announcementMapper.selectById(id);
        if (announcement == null) {
            throw new RuntimeException("公告不存在");
        }

        // 增加浏览次数
        announcementMapper.incrementViewCount(id);

        // 转换为VO
        AnnouncementVO vo = new AnnouncementVO();
        BeanUtils.copyProperties(announcement, vo);
        // 更新浏览次数
        vo.setViewCount(announcement.getViewCount() + 1);

        return vo;
    }

    @Override
    @Transactional
    public void deleteAnnouncement(Long id, Long userId) {
        // 查询公告
        Announcement announcement = announcementMapper.selectById(id);
        if (announcement == null) {
            throw new RuntimeException("公告不存在");
        }

        // 检查权限：只有发布者本人或管理员可以删除
        User user = userMapper.findById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 判断是否有删除权限
        if (!announcement.getPublisherId().equals(userId) && user.getUserType() != 0) {
            throw new RuntimeException("无权限删除该公告");
        }

        // 删除公告
        announcementMapper.deleteById(id);
    }
}

